package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.bean.BeanUtils;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizGoodsSku;
import com.member.shop.domain.SysBizUser;
import com.member.shop.domain.SysBizUserCollect;
import com.member.shop.dto.CollectDto;
import com.member.shop.mapper.SysBizGoodsSkuMapper;
import com.member.shop.mapper.SysBizUserCollectMapper;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizUserCollectService;
import com.member.shop.service.ISysBizUserService;
import com.member.shop.vo.GoodsIndexVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 用户收藏Service业务层处理
 *
 * @author wangxin
 * @date 2024-01-17
 */
@Service
public class SysBizUserCollectServiceImpl extends ServiceImpl<SysBizUserCollectMapper, SysBizUserCollect> implements ISysBizUserCollectService {
    @Resource
    private SysBizUserCollectMapper sysBizUserCollectMapper;
    @Resource
    private ISysBizGoodsService sysBizGoodsService;
    @Resource
    private SysBizGoodsSkuMapper sysBizGoodsSkuMapper;
    @Resource
    private ISysBizUserService sysBizUserService;

    /**
     * 查询用户收藏
     *
     * @param id 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public SysBizUserCollect selectSysBizUserCollectById(Long id) {
        return sysBizUserCollectMapper.selectById(id);
    }

    /**
     * 查询用户收藏列表
     *
     * @return 用户收藏
     */
    @Override
    public List<SysBizUserCollect> selectSysBizUserCollectList(LambdaQueryWrapper<SysBizUserCollect> queryWrapper) {
        return sysBizUserCollectMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户收藏
     *
     * @param sysBizUserCollect 用户收藏
     * @return 结果
     */
    @Override
    public int insertSysBizUserCollect(SysBizUserCollect sysBizUserCollect) {
        return sysBizUserCollectMapper.insert(sysBizUserCollect);
    }

    /**
     * 修改用户收藏
     *
     * @param sysBizUserCollect 用户收藏
     * @return 结果
     */
    @Override
    public int updateSysBizUserCollect(SysBizUserCollect sysBizUserCollect) {
        return sysBizUserCollectMapper.updateById(sysBizUserCollect);
    }

    /**
     * 批量删除用户收藏
     *
     * @param ids 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserCollectByIds(Long[] ids) {
        return sysBizUserCollectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户收藏信息
     *
     * @param id 用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserCollectById(Long id) {
        return sysBizUserCollectMapper.deleteById(id);
    }

    @Override
    public List<GoodsIndexVo> queryCollectByUser(List<Long> goodsIdList, SysBizUser loginUser) {
        List<GoodsIndexVo> goodsIndexVoList = new ArrayList<>();
        for (Long id : goodsIdList) {
            SysBizGoods goods = sysBizGoodsService.getById(id);
            if (goods == null || goods.getGrounding() == 1) {
                continue;
            }
            GoodsIndexVo goodsIndexVo = new GoodsIndexVo();
            BeanUtils.copyProperties(goods, goodsIndexVo);
            //List<SysBizGoodsSku> sysBizGoodsSkus = sysBizGoodsSkuMapper.selectList(new LambdaQueryWrapper<SysBizGoodsSku>().eq(SysBizGoodsSku::getGoodsId, goods.getId()));
            //sysBizGoodsSkus.sort(Comparator.comparing(SysBizGoodsSku::getNoMemberSalesPrice));
            //BigDecimal salesPrice;
            //BigDecimal marketPrice;
            //SysBizGoodsSku sysBizGoodsSku = sysBizGoodsSkus.get(0);
            //if (loginUser == null || (loginUser.getIsRegisterMember() == 0 && loginUser.getProcessStatus() != 2)) {
            //    salesPrice = sysBizGoodsSku.getNoMemberSalesPrice();
            //    marketPrice = sysBizGoodsSku.getNoMemberMarketPrice();
            //} else {
            //    salesPrice = sysBizGoodsSku.getMemberSalesPrice();
            //    marketPrice = sysBizGoodsSku.getMemberMarketPrice();
            //}
            //goodsIndexVo.setSalesPrice(salesPrice);
            //goodsIndexVo.setMarketPrice(marketPrice);
            goodsIndexVoList.add(goodsIndexVo);
        }
        return goodsIndexVoList;
    }

    @Override
    public void collect(CollectDto collectDto, Long userId) {
        sysBizUserCollectMapper.insert(SysBizUserCollect.builder()
                .userId(userId)
                .type(collectDto.getType())
                .goodsId(collectDto.getId()).build());
    }
}
